package net.zretc.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import net.zretc.entitys.Users;
import net.zretc.utils.DBUtil;

public class UserDAOImpl implements UsersDAO{
	private DBUtil dbUtil=new DBUtil();
	
	@Override
	public Users findByPhoneAndPassword(String username, String password) {
		String sql="select * from tab_users where username=? and pwd=?";
		List<Users> queryForList = dbUtil.queryForList(sql, Users.class, username,password);
		Users users1=null;
		for (Users users : queryForList) {
			users1=users;
		}
			return users1;
		
	}
	@Override
	public int insert(Users users) {
		String sql="insert into tab_users(username,pwd,realname,phone,address) values(?,?,?,?,?)";
		int i=dbUtil.update(sql, users.getUsername(),users.getPwd(),users.getRealname(),users.getPhone(),users.getAddress());
		return i;
	}
	@Override
	public int update(Users users) {
		String sql="update tab_users set username=?,pwd=?,realname=?,phone=?,address=? where id=?";
		int i=dbUtil.update(sql, users.getUsername(),users.getPwd(),users.getRealname(),users.getPhone(),users.getAddress(),users.getId());
		return i;
	}
	@Override
	public List<Users> findAll() {
		// TODO Auto-generated method stub
		return dbUtil.queryForList("select * from tab_users", Users.class);
	}
	@Override
	public int delete(int id) {
		// TODO Auto-generated method stub
		String sql="delete from tab_users where id=?";
		int i=dbUtil.update(sql, id);
		return i;
	}
	@Override
	public Users findById(int id) {
		// TODO Auto-generated method stub
		String sql="select * from tab_users where id=?";
		List<Users> queryForList = dbUtil.queryForList(sql, Users.class, id);
		Users users1=null;
		for (Users users : queryForList) {
			users1=users;
		}
			return users1;
	}
	@Override
	public int Count() {
		String sql="select count(*) from tab_users";
		ResultSet resultSet=dbUtil.query(sql);
		int count=0;
		try {
			while(resultSet.next()){
				count=resultSet.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return count;
	}

}
